
global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/make_final_dataset_hinvt_hq.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {


qui do ${code_dir}/config/country_list.do
qui do ${code_dir}/config/labeling_finalvars.do
global sectors MANUF TOTAL

use ${dataset_dir}/dep_vars/bvd_year_depvars.dta, clear
mmerge BvD year using ${dataset_dir}/dep_vars/bvd_year_list${weight_window}_${wtype}.dta, unmatched(none)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_devgdp_sharesgdpweighted${weight_window}_${wtype}_invt_hctry_restricted.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_wages_vaemp_totind_sharesgdpweighted${weight_window}_${wtype}_invt_hctry_restricted.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_gdpcap_manuf_sharesgdpweighted${weight_window}_${wtype}_invt_hctry_restricted.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_wages_vaemp_manuf_sharesgdpweighted${weight_window}_${wtype}_invt_hctry_restricted.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_gdpcap_totind_sharesgdpweighted${weight_window}_${wtype}_invt_hctry_restricted.dta, unmatched(master)
	

if "$weight_window" == "_from1970" && "$wtype" == "tfacit1" {
	mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_lintr_manuf_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
}

mmerge BvD using ${final_dir}/BvD_industry.dta, unmatched(master)
replace industry = "NA" if _m == 1
drop _m


*lngdp_gap averages

ren lngdpgap_ALL_1995_wtd lngdpgap_1995_a



foreach vr in shr_hinvt shr_fginvt shr2_hinvt shr2_fginvt {
	ren lngdpgap_`vr'_1995_wtd lngdpgap_`vr'_1995_a
}


*Logs for the rest
**********************************************

foreach v in lsw hsw {
	gen `v'MPm_1995_a = ln(`v'MPm_ALL_1995_wtd )
	ren ln`v'MPm_ALL_1995_wtd ln`v'MPm_1995_a
	gen `v'MPt_1995_a = ln(`v'MPt_ALL_1995_wtd )
	ren ln`v'MPt_ALL_1995_wtd ln`v'MPt_1995_a
}


gen gdppcMPm_1995_a = ln(gdppcMPm_ALL_1995_wtd )
gen gdppcMPt_1995_a = ln(gdppcMPt_ALL_1995_wtd )
ren lngdppcMPm_ALL_1995_wtd lngdppcMPm_1995_a
ren lngdppcMPt_ALL_1995_wtd lngdppcMPt_1995_a

gen vaempMPm_1995_a = ln(vaempMPm_ALL_1995_wtd )
ren lnvaempMPm_ALL_1995_wtd lnvaempMPm_1995_a
gen vaempMPt_1995_a = ln(vaempMPt_ALL_1995_wtd )
ren lnvaempMPt_ALL_1995_wtd lnvaempMPt_1995_a


*averages for the rest of the variables

foreach v in lsw hsw vaemp {
	foreach vr in shr_hinvt shr_fginvt shr2_hinvt shr2_fginvt { 
		cap gen `v'MPm_`vr'_1995_a = ln(`v'MPm_`vr'_1995_wtd)
	}
}

foreach vr in shr_hinvt shr_fginvt shr2_hinvt shr2_fginvt {
	gen gdppcMPm_`vr'_1995_a = ln(gdppcMPm_`vr'_1995_wtd)
}


qui ds *_shr_hinvt_1995_a 

foreach vaar in `r(varlist)' {
	local vv = substr("`vaar'",1,strpos("`vaar'","_")-1)
	if "`vv'" == "lngdpgap" { 
		continue
	}

	*normalize
	gen term_hinvt_var = exp(`vv'_shr2_hinvt_1995_a)/exp(`vv'_1995_a)*maxweight_1995
	gen term_fginvt_var = exp(`vv'_shr2_fginvt_1995_a)/exp(`vv'_1995_a)*(1-maxweight_1995)
	gen _term_hinvt = term_hinvt_var if year==1995
	gen _term_fginvt = term_fginvt_var if year==1995
	bys lse_id : egen term_hinvt_fixed = max(_term_hinvt)
	bys lse_id : egen term_fginvt_fixed = max(_term_fginvt)
	gen `vv'_shr4_hinvt_1995_a = `vv'_shr2_hinvt_1995_a*term_hinvt_fixed
	gen `vv'_shr4_fginvt_1995_a = `vv'_shr2_fginvt_1995_a*term_fginvt_fixed
	drop term_* _term_*
}


keep BvDIDnumber year lngdpgap_shr_fginvt_1995_a lswMPm_shr4_fginvt_1995_a hswMPm_shr4_fginvt_1995_a vaempMPm_shr4_fginvt_1995_a gdppcMPm_shr4_fginvt_1995_a

*labeling
labelingvarsfinal "lsw"
labelingvarsfinal "hsw"
labelingvarsfinal "vaemp"
labelingvarsfinal "gdppc"
labelingvarsfinal "lngdpgap"

compress
save ${final_dir}/supplement_dataset${weight_window}_${wtype}_hinvt_ctry_restricted.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat